package com.fightingFish.user.servlet;

import com.fightingFish.user.bean.User;
import com.fightingFish.user.service.impl.UserServiceImpl;
import net.sf.json.JSONObject;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

@WebServlet("/userRegisterAndLogin")
public class RegisterAndLogin extends HttpServlet {

    //创建UserServiceImpl对象
    private UserServiceImpl usi = new UserServiceImpl();

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        //1.设置信息获取的编码
        req.setCharacterEncoding("utf-8");
        //2.设置返回文件的类型和编码
        resp.setCharacterEncoding("utf-8");
        resp.setContentType("text/html");
        //3.获取用户请求中的方法
        String methodName = req.getParameter("methodName");
        //4.利用反射获取方法
        try {
            Method method = this.getClass().getDeclaredMethod(methodName, HttpServletRequest.class, HttpServletResponse.class);
            //5.调用该方法
            method.invoke(this, req, resp);
        } catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException e) {
            e.printStackTrace();
        }
    }

    /**
     * 方法描述：用户手机号注册时，获取验证码
     *
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    protected void phoneRegisterVeriCode(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("phoneRegisterVeriCode");
        //1.获取用户经由前端发送来的手机号
        String phone = req.getParameter("phone");
        //2.调用service层的第三方（秒滴）发送短信验证码方法
        String result = usi.phoneRegisterVeriCode(phone);
        //3.将结果返回给用户
        resp.getWriter().print(result);
    }

    /**
     * 用户手机号注册
     *
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    protected void phoneRegister(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("phoneRegister");
        //获取用户经由前端发送来的手机号和验证码
        String phone = req.getParameter("phone");
        String veriCode = req.getParameter("veriCode");
        //调用service层的用户注册功能
        boolean result = usi.phoneRegister(phone, veriCode);
        if (result) {
            resp.getWriter().print("注册成功");
            resp.sendRedirect(req.getContextPath() + "/jsp/login.jsp");
        } else
            resp.getWriter().print("注册成功");
    }

    /**
     * 方法描述：用户手机号注册时，获取验证码
     *
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    protected void phoneLoginVeriCode(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("phoneLoginVeriCode");
        //1.获取用户经由前端发送来的手机号
        String phone = req.getParameter("phone");
        //2.调用service层的第三方（秒滴）发送短信验证码方法
        String result = usi.phoneLoginVeriCode(phone);
        //3.将结果返回给用户
        resp.getWriter().print(result);
    }

    /**
     * 方法概述：   验证登录
     *
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    protected void phoneLogin(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("phoneLogin");
        //获取用户经由前端发送来的手机号和密码
        String phone = req.getParameter("phone");
        String veriCode = req.getParameter("veriCode");
        //调用service层的用户注册功能
        User result = usi.phoneLogin(phone, veriCode);
        System.out.println(result);
        if (result != null) {
            //登录成功
            System.out.println("登录成功");
            req.getSession().setAttribute("loginUser", result);
//            往域里面放一个手机号  方便使用
            req.getSession().setAttribute("phone",phone);
            resp.sendRedirect(req.getContextPath() + "/anchorSearch?methodName=queryAnchorByType");
        } else
            System.out.println("登录失败");
    }

    protected void upDatePrice(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("upDatePrice");
        //获取用户登录的手机号号
        String phone = (String) req.getSession().getAttribute("phone");
// 获取当前主播的ID
        int userId = Integer.parseInt(req.getParameter("userId"));
//        获取当前礼物的金额
        Double price = Double.valueOf(req.getParameter("price"));
//      获取主播余额
        Double balance = Double.valueOf(req.getParameter("balance"));
        resp.getWriter().print(usi.upDatePrice(phone,price,userId,balance));



    }
}
